//
//  AddCaseTableViewController.m
//  QuickCure
//
//  Created by 雪中狼 on 9/13/16.
//  Copyright © 2016 haoge. All rights reserved.
//

#import "AddCaseTableViewController.h"
#import "CaseModel.h"
#import "SonTableViewCell1.h"
#import "SonTableViewCell2.h"
#import "SonTableViewCell3.h"
#import "MainListTableViewController.h"
#import "SonListTableViewController.h"
#import <SVProgressHUD.h>

static NSString *identifier1 = @"cell1";
static NSString *identifier2 = @"cell2";
static NSString *identifier3 = @"cell3";
@interface AddCaseTableViewController ()<SonTableViewCell3Delegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
//@property(nonatomic,strong)NSMutableArray *modelList;
@property (nonatomic,copy)NSString *mainName;
@property (nonatomic,copy)NSString *sonName;
@property(nonatomic,strong)CaseModel *caseModel;
@property(nonatomic,strong)UIImage* currentPic;
@property(nonatomic,strong)UIImageView * selectedPic;






@end

@implementation AddCaseTableViewController

-(CaseModel *)caseModel{
    if (nil == _caseModel) {
        _caseModel = [[CaseModel alloc] init];
    }
    return _caseModel;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.tableView reloadData];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    UIImage *image = [UIImage imageNamed:@"jiantou"];
    
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(popback)];

    UIBarButtonItem *upload = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self action:@selector(uploadClick)];
    self.navigationItem.rightBarButtonItem = upload;
    [self.tableView registerClass:[SonTableViewCell1 class] forCellReuseIdentifier:identifier1];
    [self.tableView registerClass:[SonTableViewCell2 class] forCellReuseIdentifier:identifier2];
    [self.tableView registerClass:[SonTableViewCell3 class] forCellReuseIdentifier:identifier3];
    self.tableView.estimatedRowHeight = 70;
    self.tableView.rowHeight = UITableViewAutomaticDimension;

    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
    
    
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        SonTableViewCell1 *cell1 = [tableView dequeueReusableCellWithIdentifier:identifier1 forIndexPath:indexPath];
        if (indexPath.row == 0) {
            cell1.leftLabel.text = @"疾病类型:";
        } else {
            cell1.leftLabel.text = @"疾病细分:";
        }
        if (self.mainName && indexPath.row == 0) {
            cell1.rightLabel.text = self.mainName;
        }else if(self.sonName && indexPath.row == 1){
            cell1.rightLabel.text = self.sonName;
        }        return cell1;
    }
    if (indexPath.section ==1) {
        if (indexPath.row == 0) {
            SonTableViewCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:identifier2 forIndexPath:indexPath];
            [cell2 setSelectionStyle:UITableViewCellSelectionStyleNone];
            cell2.describeBlock = ^(NSString *str){
                self.caseModel.illnessDescribe = str;
            } ;
            return cell2;

        }
        SonTableViewCell3 *cell3 = [tableView dequeueReusableCellWithIdentifier:identifier3 ];
//        cell =[tableView dequeueReusableCellWithIdentifier:@"picture"];
        [cell3 setSelectionStyle:UITableViewCellSelectionStyleNone];

        SonTableViewCell3 *uCell = (SonTableViewCell3*)cell3;
        uCell.delegate=self;
        self.selectedPic=uCell.selectedPic;
        
        return cell3;
    }
   
    return nil;
    }

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section ==1) {
        return 200;
    }
    return 60;
}




-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.section == 1) {
        
        return;
    }

    if (indexPath.section ==0) {
        if (indexPath.row == 0) {
            MainListTableViewController *vc =[[MainListTableViewController alloc]init];
            vc.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"home_nav_button_back"] style:UIBarButtonItemStylePlain target:self action:@selector(popback)];
            vc.mainCaseBlock =^(NSString *str){
                self.mainName = str;
                self.caseModel.illnessType = str;
            };
            vc.navigationItem.title = @"疾病类型";
            [self.navigationController pushViewController:vc animated:YES];

        }else{
            
            if (!self.mainName) {
                [SVProgressHUD showInfoWithStatus:@"请选择大模块"];
                
                [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [SVProgressHUD dismiss];
                });
                return;
                
            }

            
            SonListTableViewController *vc =[[SonListTableViewController alloc]init];
            vc.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"home_nav_button_back"] style:UIBarButtonItemStylePlain target:self action:@selector(popback)];
            vc.sonCaseBlock =^(NSString *str){
                self.sonName = str;
                self.caseModel.basicType =str;
            };
            vc.navigationItem.title = @"疾病细分";
            [self.navigationController pushViewController:vc animated:YES];

        }
        

    }

    
}
-(void)popback{
    
    
    [self.navigationController popViewControllerAnimated:YES];

    
    
    
}
-(void)uploadClick{
    if (!self.sonName) {
        if (!self.mainName) {
            [SVProgressHUD showInfoWithStatus:@"请选择具体疾病细分"];
            
            [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });
            return;
            
        }
        return;
    }
    
    
    
    
    NSDate * date = [NSDate date];
    NSTimeInterval sec = [date timeIntervalSinceNow];
    NSDate * currentDate = [[NSDate alloc] initWithTimeIntervalSinceNow:sec];
    
    NSDateFormatter * df = [[NSDateFormatter alloc] init ];
    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString * na = [df stringFromDate:currentDate];
    
    self.caseModel.time = na;
    self.caseModel.photoImage =self.currentPic;

    if(self.caseListBlock){
        
        self.caseListBlock(self.caseModel);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
    
}
-(UIImage *)presentPickerImageControllerWithcell:(SonTableViewCell3 *)cell{
    
    UIImagePickerController * picker =[[UIImagePickerController alloc]init];
    
    picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    
    picker.delegate=self;
    [self presentViewController:picker animated:YES completion:^{
        
    }];
    return nil;
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
//    NSLog(@"%@",info);
    
    UIImage * selectedImg = info[@"UIImagePickerControllerOriginalImage"];
    
    [self dismissViewControllerAnimated:YES completion:^{
        self.currentPic=selectedImg;
        self.selectedPic.image=self.currentPic;
        
    }];
}
-(instancetype)init{
        return [self initWithStyle:UITableViewStyleGrouped];

}


@end
